//
//  ZyaFeedOrderCell.swift
//  SlaughterClient
//
//  Created by Zhouweibin on 2018/1/26.
//  Copyright © 2018年 zwb. All rights reserved.
//

import UIKit

class ZyaFeedOrderCell: UITableViewCell {
    
    var selectGoodsAction: (() -> Void)?
    
    var model: ZyaFeedOrder? {
        didSet {
            productLabel.text = model?.brandNm
            unitPriceLabel.text = model?.brandSpec
            goodsLabel.text = model?.cbTypeNm
            
            if let isSelected = model?.isSelected, isSelected {
                selectedButton.setImage(#imageLiteral(resourceName: "select"), for: UIControl.State())
            } else {
                selectedButton.setImage(#imageLiteral(resourceName: "unselect"), for: UIControl.State())
            }
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        backgroundColor = Theme.backgroudColor
        selectionStyle = .none
        
        addSubview(bgView)

        bgView.addSubview(productLabel)
        bgView.addSubview(goodsLabel)
        bgView.addSubview(unitPriceLabel)
        bgView.addSubview(selectedButton)
        
        selectedButton.addTarget(self, action: #selector(selectGoods), for: .touchUpInside)
        
        setupConstraints()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func selectGoods() {
        model?.isSelected = !(model?.isSelected)!
        if let action = selectGoodsAction {
            action()
        }
    }
    
    private func setupConstraints () {
        
        bgView.snp.makeConstraints { (maker) in
            maker.edges.equalTo(UIEdgeInsets(top: 4, left: 8, bottom: 4, right: 8))
            maker.height.equalTo(60)
        }
        
        selectedButton.snp.makeConstraints { (maker) in
            maker.left.equalTo(8)
            maker.size.equalTo(CGSize(width: 25, height: 25))
            maker.centerY.equalToSuperview()
        }
        
        productLabel.snp.makeConstraints { (maker) in
            maker.left.equalTo(selectedButton.snp.right).offset(8)
            maker.centerY.equalToSuperview()
        }
        
        unitPriceLabel.snp.makeConstraints { (maker) in
            maker.left.equalTo(productLabel.snp.right).offset(8)
            maker.right.equalTo(goodsLabel.snp.left).offset(-8)
            maker.width.equalTo(90)
            maker.centerY.equalToSuperview()
        }
        
        goodsLabel.snp.makeConstraints { (maker) in
            maker.right.equalTo(-8)
            maker.width.equalTo(60)
            maker.centerY.equalToSuperview()
        }
    }

    lazy var selectedButton: UIButton = {
        let button = UIButton()
        button.setImage(#imageLiteral(resourceName: "unselect"), for: UIControl.State())
        return button
    } ()
    
    lazy var bgView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.white
        return view
    } ()
    
    lazy var productLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.textColor
        label.text = "222-YN-SN"
        label.numberOfLines = 0
        label.font = UIFont.boldSystemFont(ofSize: 13)
        return label
    } ()
    
    lazy var goodsLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.textColor
        label.text = "教槽料"
        label.numberOfLines = 0
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 13)
        return label
    } ()
    
    lazy var unitPriceLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.textColor
        label.text = "单价：120元/包"
        label.numberOfLines = 0
        label.textAlignment = .center
        label.font = UIFont.systemFont(ofSize: 13)
        return label
    } ()
    
}
